import java.util.ArrayList;

/*This class is the main class for running on multiple machines.
 * As an argument one must provide the index of that machine in the machines.txt
 * file. If the node is the starter node, a second argument is required.
 */
public class FailureDetector {


	public static void main(String[] args) {

		int index = -1;

		if (args.length == 0) {
			System.out.println("Provide which index machine is as an argument.");
			System.exit(0);
		}
				
		if(args.length == 2) {
			Global.starter = true;
		}
		index = Integer.parseInt(args[0]); //get machine index
		String filename = "machines.txt";
		ArrayList<String> data = (new FileRead(filename)).getLines();
		ArrayList<MachineData> mData = new ArrayList<MachineData>();

		for(String s: data) {
			mData.add(new MachineData(s));
		}

		Machine m = new Machine(mData, index);

		m.start();

		long t = System.currentTimeMillis();
		long finish = t + Global.simLength;

		while(true) { //run simulation until its over and then print results.
			t = System.currentTimeMillis();

			if (t >= finish) {
				System.out.println("Total messages and bandwidth (bytes) : " + Global.numMessages);
				System.out.println("Total messages per second : " + ((float) Global.numMessages) / (Global.simLength/1000 - Global.grace));
				System.out.println("Total bandwith : " + ((float) Global.numMessages) / (Global.simLength/1000 - Global.grace));
				System.out.println("Total number of failures : " + Global.numFailures);
				System.exit(0);
			}
		}
	}
}
